package cn.edu.hbvtc.qms.web.struts.action;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.edu.hbvtc.qms.domain.Clazz;
import cn.edu.hbvtc.qms.domain.ClazzArrangement;
import cn.edu.hbvtc.qms.domain.Department;
import cn.edu.hbvtc.qms.domain.EvalPeriod;
import cn.edu.hbvtc.qms.domain.Evaluation;
import cn.edu.hbvtc.qms.domain.Major;
import cn.edu.hbvtc.qms.domain.Role;
import cn.edu.hbvtc.qms.domain.Teacher;
import cn.edu.hbvtc.qms.domain.Term;
import cn.edu.hbvtc.qms.domain.User;
import cn.edu.hbvtc.qms.service.IEvaluationService;
import cn.edu.hbvtc.qms.service.IManagementService;

public class DropChangeAJAXAction extends AbstractBaseDispatchAction {

	private IEvaluationService evaluationService = null;
	private IManagementService managementService = null;

	public IEvaluationService getEvaluationService() {
		return evaluationService;
	}

	public void setEvaluationService(IEvaluationService evaluationService) {
		this.evaluationService = evaluationService;
	}

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 通过下拉列表中的院系ID，获得该院系的所有专业
	public ActionForward dropChangeOfMajor(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String deptId = request.getParameter("deptId");
		Department dept = managementService.findDepartmentById(Integer
				.parseInt(deptId));
		List<Major> majorList = managementService.findMajorsOfDept(dept);

		// 以下为返回到前台页面html数据
		StringBuffer sb = new StringBuffer();
		sb.append("document.forms[0].majorId.length = "
				+ (majorList.size() + 1) + ";\n");
		sb.append("document.forms[0].majorId.options[0].value = '0';\n");
		sb.append("document.forms[0].majorId.options[0].text = '请选择';\n");
		sb.append("document.forms[0].majorId.selectedIndex = 0;\n");

		for (int i = 0; i < majorList.size(); i++) {
			Major m = majorList.get(i);
			sb.append("document.forms[0].majorId.options[" + (i + 1)
					+ "].value = '" + m.getId() + "';\n");
			sb.append("document.forms[0].majorId.options[" + (i + 1)
					+ "].text = '" + m.getMajorName() + "';\n");
		}

		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		response.getWriter().write(sb.toString());

		return null;
	}

	// 通过下拉列表中的专业ID，获得该专业所有班级
	public ActionForward dropChangeOfClazz(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String majorId = request.getParameter("majorId");
		Major major = managementService
				.findMajorById(Integer.parseInt(majorId));
		List<Clazz> clazzList = managementService.findClazzsOfMajor(major);

		// 以下为返回到前台页面html数据
		StringBuffer sb = new StringBuffer();
		sb.append("document.forms[0].clazzId.length = "
				+ (clazzList.size() + 1) + ";\n");
		sb.append("document.forms[0].clazzId.options[0].value = '0';\n");
		sb.append("document.forms[0].clazzId.options[0].text = '请选择';\n");
		sb.append("document.forms[0].clazzId.selectedIndex = 0;\n");

		for (int i = 0; i < clazzList.size(); i++) {
			Clazz c = clazzList.get(i);
			sb.append("document.forms[0].clazzId.options[" + (i + 1)
					+ "].value = '" + c.getId() + "';\n");
			sb.append("document.forms[0].clazzId.options[" + (i + 1)
					+ "].text = '" + c.getClazzId() + "';\n");
		}

		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		response.getWriter().write(sb.toString());

		return null;
	}

	// 通过下拉列表中的部门ID，获得该部门的所有教师
	public ActionForward changeDropOfTeacher(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		User user = managementService.findUserByUsername(this.getPrincipal().getUsername());
		
		Role role1 = managementService.findRoleByName("TEACHER");
		Role role2 = managementService.findRoleByName("ADMIN");
		Role role3 = managementService.findRoleByName("DEPT_ADMIN");
		Role role4 = managementService.findRoleByName("LEADER");
		Role role5 = managementService.findRoleByName("DEPT_LEADER");
		
		String deptId = request.getParameter("deptId");
		List<Teacher> teacherList = new ArrayList<Teacher>();
		
		if (user.getRoles().contains(role3) || user.getRoles().contains(role5)) {
			//部门管理员和领导可以查询本部门所有教师的课程评价历史数据
			Department dept = managementService.findDepartmentById(Integer.parseInt(deptId));
			teacherList = managementService.findTeachersOfDept(dept);
		} else if (user.getRoles().contains(role2) || user.getRoles().contains(role4)) {
			//超级管理员和学校领导可以查询本校所有部门的相关教师的课程评价历史数据
			Department dept = managementService.findDepartmentById(Integer.parseInt(deptId));
			teacherList = managementService.findTeachersOfDept(dept);
		} else if (user.getRoles().contains(role1)) {
			//教师用户只能查询自己的课程评价历史数据
			Teacher teacher = managementService.findTeacherById(user.getId());
			teacherList.add(teacher);
		} 
		
		// 以下为返回到前台页面html数据
		StringBuffer sb = new StringBuffer();
		sb.append("document.forms[0].teacherId.length = "
				+ (teacherList.size() + 1) + ";\n");
		sb.append("document.forms[0].teacherId.options[0].value = '0';\n");
		sb.append("document.forms[0].teacherId.options[0].text = '请选择';\n");
		sb.append("document.forms[0].teacherId.selectedIndex = 0;\n");

		for (int i = 0; i < teacherList.size(); i++) {
			Teacher t = teacherList.get(i);
			sb.append("document.forms[0].teacherId.options[" + (i + 1)
					+ "].value = '" + t.getId() + "';\n");
			sb.append("document.forms[0].teacherId.options[" + (i + 1)
					+ "].text = '" + t.getNickname() + "';\n");
		}

		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		response.getWriter().write(sb.toString());

		return null;
	}
	
	// 通过下拉列表中的部门ID和学期ID，获得该部门的所有评价时间列表
	public ActionForward changeDropOfEvalPeriod(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		User user = managementService.findUserByUsername(this.getPrincipal().getUsername());
		
		String deptId = request.getParameter("deptId");
		String termId = request.getParameter("termId");
		
		List<EvalPeriod> epList = new ArrayList<EvalPeriod>();
		

		Department dept = managementService.findDepartmentById(Integer.parseInt(deptId));
		Term term = managementService.findTermById(Integer.parseInt(termId));
		
		epList = evaluationService.findEvalPeriodsInDeptAndTerm(dept,term);

		
		// 以下为返回到前台页面html数据
		StringBuffer sb = new StringBuffer();
		sb.append("document.forms[0].evalPeriodId.length = "
				+ (epList.size() + 1) + ";\n");
		sb.append("document.forms[0].evalPeriodId.options[0].value = '0';\n");
		sb.append("document.forms[0].evalPeriodId.options[0].text = '请选择';\n");
		sb.append("document.forms[0].evalPeriodId.selectedIndex = 0;\n");

		for (int i = 0; i < epList.size(); i++) {
			EvalPeriod ep = epList.get(i);
			sb.append("document.forms[0].evalPeriodId.options[" + (i + 1)
					+ "].value = '" + ep.getId() + "';\n");
			sb.append("document.forms[0].evalPeriodId.options[" + (i + 1)
					+ "].text = '" + ep.getStartDate() + "至" + ep.getEndDate() + "';\n");
		}

		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);

		response.getWriter().write(sb.toString());

		return null;
	}
	
}